from src.util.Route import *
from src.service import MenuManageService


# 查询所有的菜单 （分页）
@route("/menu/all")
def menuGetAll():
    r = MenuManageService.getMenuAll()
    return R.success(r)


# 查询所有的菜单 (分页 查询id 菜单名)
@route("/menu/get/name", methods=['POST'])
def menuGetName():
    data = request.json
    num = data.get("num")
    size = data.get("size")
    page = Page(num=num, size=size)  # Create a Page object
    r = MenuManageService.getMenuName(page)
    return R.success(r)


# 修改菜单信息（除了mid）
@route("/menu/update", methods=['POST'])
def menuUpdate():
    k: dict = request.json
    r = MenuManageService.updateMenu(k)
    if r:
        return R.success(r, "菜单信息修改成功")
    else:
        return R.create(400, None, '菜单信息修改失败')


# 添加菜单
@route("/menu/add", methods=['POST'])
def menuAdd():
    data: dict = request.json
    r = MenuManageService.addMenu(data)
    if r:
        return R.success(r, "菜单添加成功")
    else:
        return R.create(400, r, '菜单添加失败')


# 删除菜单
@route("/menu/delete", methods=['POST'])
def menuDelete():
    mids = request.json
    r = MenuManageService.delMenu(mids)
    if type(r) == bool:
        if r:
            return R.success(r, "菜单删除成功")
        else:
            return R.bad_request('菜单删除失败')
    else:
        return R.bad_request(r)


# 查询菜单上的所有按钮
@route("/menu/get/handles")
def menuGetHandles():
    mid = request.args.get("mid")  # Create a Page object
    r = MenuManageService.getMenuHandles(mid)
    return R.success(r)


@route("/menu/handles/update", methods=['POST'])
def updateMenuHandle():
    data = request.json
    r = MenuManageService.updateMenuHandles(data.get('mid'), data.get('data'))
    if (r):
        return R.success(r, '操作成功')
    return R.bad_request('操作失败')
